package com.elangzhi.ssm.modules.user.services;

import com.elangzhi.ssm.tools.ProjectConfig;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Userinfos;
import com.taobao.api.request.OpenimUsersAddRequest;
import com.taobao.api.request.OpenimUsersGetRequest;
import com.taobao.api.request.OpenimUsersUpdateRequest;
import com.taobao.api.response.OpenimUsersGetResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * openIm 服务
 */
@Service
public class OpenIMService {

    @Autowired
    private ProjectConfig projectConfig;

    /**
     * 更改头像地址
     * @param userid
     * @param iconUrl
     */
    public void changeImg(String userid,String iconUrl) throws ApiException {
        String head = projectConfig.getServerHost() + iconUrl;
        Userinfos userinfos = new Userinfos();
        userinfos.setUserid(userid);
        userinfos.setIconUrl(head);
        List<Userinfos> userinfoses = new ArrayList<>();
        userinfoses.add(userinfos);
        TaobaoClient client = new DefaultTaobaoClient(projectConfig.getOpenImUrl(),
                projectConfig.getOpenImAppKey(), projectConfig.getOpenImSecret());
        OpenimUsersUpdateRequest request = new OpenimUsersUpdateRequest();
        request.setUserinfos(userinfoses);
        client.execute(request);
    }

    /**
     * 添加用户
     * @param userinfos
     * @throws ApiException
     */
    public void addUser(Userinfos userinfos) throws ApiException {
        List<Userinfos> userinfoses = new ArrayList<>();
        userinfos.setIconUrl(projectConfig.getServerHost() + "/images/account/defaultHead.jpg");
        userinfoses.add(userinfos);
        TaobaoClient client = new DefaultTaobaoClient(projectConfig.getOpenImUrl(),
                projectConfig.getOpenImAppKey(), projectConfig.getOpenImSecret());
        OpenimUsersAddRequest req = new OpenimUsersAddRequest();
        req.setUserinfos(userinfoses);
        client.execute(req);
    }

    /**
     * 获取用户信息
     * @param userid
     * @throws ApiException
     */
    public void getUser(String userid) throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient(projectConfig.getOpenImUrl(),
                projectConfig.getOpenImAppKey(), projectConfig.getOpenImSecret());
        OpenimUsersGetRequest req = new OpenimUsersGetRequest();
        req.setUserids(userid);
        OpenimUsersGetResponse response = client.execute(req);
        System.out.println(response.getBody());
    }


    /**
     * 更改信息
     * @param userinfos
     * @throws ApiException
     */
    public void changeUser(Userinfos userinfos) throws ApiException {

        List<Userinfos> userinfoses = new ArrayList<>();
        userinfoses.add(userinfos);

        TaobaoClient client = new DefaultTaobaoClient(projectConfig.getOpenImUrl(),
                projectConfig.getOpenImAppKey(), projectConfig.getOpenImSecret());
        OpenimUsersUpdateRequest request = new OpenimUsersUpdateRequest();
        request.setUserinfos(userinfoses);
        client.execute(request);
    }
}
